# data load and setup -----------------------------------------------------

#script for secondary analyses and regressions

rm(list=ls())    

Statelevel <- read.csv("~/Documents/Dropbox/Phillips Lax Democratic Deficit current/statelevel_2013.csv")
Statelevel <- subset(Statelevel, sstate != "DC")

options(digits=2)


library(foreign)
library(blme)
library(arm)
library(plyr)
library(gdata)
library(reshape2)
library(ggplot2)
library(rlist)
#library(windowsFonts)
source("~/Documents/Dropbox/RSF Ineqaulity Project/ggplot_theme.R")

#this is our normal flat data frame -- every vote gets one row -- if no vote, even the opinion data isn't here.  So this could be a bit different than adam's opinion summaries -- each state will show up twice here if both senators cast a vote

vote.level <- read.csv("~/Documents/Dropbox/RSF Ineqaulity Project/Replication File/flat_df_for_jeff.csv", stringsAsFactors = FALSE)




vote.level <- within(vote.level,{
    label <- issue
    year <- as.numeric(substr(issue, nchar(issue) -4 , nchar(issue) - 1))
    partisan <- ifelse(party == "D",  Dem, Rep) 
    opp.partisan <-   ifelse(party == "D",  Rep, Dem) 
    Q5yes <- Q5 > .5
    Q1yes <- Q1 > .5
    Q3yes <- Q3 > .5
    Pyes <- partisan > .5
    opp.Pyes <- opp.partisan > .5
    repdum <- ifelse(party == "R", 1, 0)
    demdum <- ifelse(party == "D", 1, 0)
#    party_SW_per <- ifelse(party == "D", Dem_SW_per, Rep_SW_per)
    party_SW_per <- NA
    party.issue.factor <- paste(party, issue)
    SWyes <- SW > .5
    cong <- (SWyes == vote)
    congrich <- (Q5yes == vote)
    congpoor <- (Q1yes == vote)
    congpart <- (Pyes == vote)
    congQ3 <- (Q3yes == vote)
    congopppart <- (opp.Pyes == vote)
    cong.rich.over.part <- ifelse(congrich == congpart, NA, congrich * (1 - congpart))
    cong.poor.over.part <- ifelse(congpoor == congpart, NA, congpoor * (1 - congpart))
    cong.rich.over.poor <- ifelse(congrich == congpoor, NA, congrich * (1 - congpoor))
    share.support.Q5 <- (.2 * Q5) / SW
    share.support.Q4 <- (.2 * Q4) / SW
    share.support.Q3 <- (.2 * Q3) / SW
    share.support.Q2 <- (.2 * Q2) / SW
    share.support.Q1 <- (.2 * Q1) / SW
    rep.control.senate <- ifelse(year <= 2006 | year > 2014, 1, 0)
})


#this is the above re-aggregated to the senator level, averaged over his or her voting record
senator.level <- with(vote.level,{
cong <- tapply(cong, senator, mean)
congrich <- tapply(congrich, senator, mean)
congpoor <- tapply(congpoor, senator, mean)
congpart <- tapply(congpart, senator, mean)
congopppart <- tapply(congopppart, senator, mean)
cong.rich.over.part <- tapply(cong.rich.over.part, senator, mean, na.rm = TRUE)
cong.poor.over.part <- tapply(cong.poor.over.part, senator, mean, na.rm = TRUE)
cong.rich.over.poor <- tapply(cong.rich.over.poor, senator, mean, na.rm = TRUE)
repdum <- tapply(repdum, senator, max)
party <- ifelse(repdum == 1, "R", "D")
partycolor <- ifelse(repdum == 1, "red", "blue")
party_SW_per <-   tapply(party_SW_per, senator, mean)
data.frame(cong, congrich, congpoor, congpart, congopppart, cong.rich.over.part, cong.poor.over.part ,cong.rich.over.poor, repdum, party, partycolor, party_SW_per )
})





#this is the vote level data reaggregated to the roll call  or issue level, with currently 39 such roll call
issue.level <- with(vote.level,{
    Dem <- tapply(Dem, issue, mean)
    Rep <- tapply(Rep, issue, mean)
    SW <- tapply(SW, issue, mean)
    year <- tapply(year, issue, mean)
    rep.control.senate <- tapply(rep.control.senate, issue, mean)
    Q5 <- tapply(Q5, issue, mean)
    Q1 <- tapply(Q1, issue, mean)
    vote.total <- tapply(vote, issue, sum)
    anti.vote.total <- tapply(1 - vote,issue,sum)
    vote.total.dem <- tapply(vote * demdum, issue, sum)
    vote.total.rep <- tapply(vote * repdum, issue, sum)
    num.dems <- tapply(demdum, issue, sum)
    num.reps <- tapply(repdum, issue, sum)
    cong <- tapply(cong, issue, mean)
    congrich <- tapply(congrich, issue, mean)
    congpoor <- tapply(congpoor, issue, mean)
    congpart <- tapply(congpart, issue, mean)
    congopppart <-  tapply(congopppart, issue, mean)
    cong.rich.over.part <- tapply(cong.rich.over.part, issue, mean, na.rm = TRUE)
    cong.poor.over.part <- tapply(cong.poor.over.part, issue, mean, na.rm = TRUE)
    cong.rich.over.poor <- tapply(cong.rich.over.poor, issue, mean, na.rm = TRUE)
    data.frame(year, Dem, Rep, SW, Q5, Q1, vote.total,anti.vote.total,vote.total.dem, vote.total.rep, num.dems, num.reps, cong, congrich, congpoor, congpart, congopppart, cong.rich.over.part, cong.poor.over.part ,cong.rich.over.poor, rep.control.senate )
})

#adds some columns summarizing issue.level data
issue.level <- within(issue.level,{
    vote.yes.dem.percent <- vote.total.dem / num.dems
    vote.yes.rep.percent <- vote.total.rep / num.reps
    opposed.parties <- ifelse((vote.yes.dem.percent > .5) == (vote.yes.rep.percent < .5), TRUE, FALSE)
    more.repvotes.issue <- ifelse(vote.yes.rep.percent > vote.yes.dem.percent, 1, 0)
    more.repsupport.issue <- ifelse(Rep > Dem, 1, 0)
    votes.support.split <- (more.repvotes.issue != more.repsupport.issue)
    more.richsupport.issue <- ifelse(Q5 > Q1, 1, 0)
})

issue.level$issue <- row.names(issue.level)

#brings some above summaries from issue.level back to the vote level data

vote.level <- join(vote.level,subset(issue.level, select = c(issue, more.repvotes.issue,more.richsupport.issue, vote.yes.dem.percent, vote.yes.rep.percent )) , by = "issue")




vote.level$vote.for.rich.side <-  ifelse(vote.level$more.richsupport.issue == 1, vote.level$vote ,  1 - vote.level$vote)
vote.level$Q5.supportforrichside <- ifelse(vote.level$more.richsupport.issue == 1, vote.level$Q5 ,  1 - vote.level$Q5)
vote.level$Q1.supportforrichside <- ifelse(vote.level$more.richsupport.issue == 1, vote.level$Q1 ,  1 - vote.level$Q1)
vote.level$partisan.supportforrichside <- ifelse(vote.level$more.richsupport.issue == 1, vote.level$partisan ,  1 - vote.level$partisan)


vote.level$vote <- abs(vote.level$vote)
vote.level <- within(vote.level, {
    libdir.Q1 <- ifelse(more.repvotes.issue == 1,   1 - Q1,  Q1)
    libdir.Q5 <- ifelse(more.repvotes.issue == 1,   1 - Q5,  Q5)
    libdir.SW <- ifelse(more.repvotes.issue == 1,   1 - SW,  SW)
    libdir.Dem <- ifelse(more.repvotes.issue == 1,   1 - Dem,  Dem)
    libdir.Rep <- ifelse(more.repvotes.issue == 1,   1 - Rep,  Rep)
    libdir.vote <- ifelse(more.repvotes.issue == 1,   1 - vote,   vote)
    own.party.issue <-  ifelse(party == "R", more.repvotes.issue, 1 - more.repvotes.issue )
    party.line.says.vote <- ifelse(party == "R", ifelse(vote.yes.rep.percent >= .5, 1, 0), ifelse(vote.yes.dem.percent >= .5, 1, 0) )
})



issue.level$partisan.diff <-  abs(issue.level$Dem - issue.level$Rep)
issue.level$class.diff <-  abs(issue.level$Q5 - issue.level$Q1)


vote.level$partisan.diff <-  abs(vote.level$Dem - vote.level$Rep)
vote.level$class.diff <-  abs(vote.level$Q5 - vote.level$Q1)



vote.level <- arrange(vote.level, issue,senator)


######   do same from uncertainty version, shrinking to medians then same code as above for that.



vote.level.sims <- read.csv("~/Documents/Dropbox/RSF Ineqaulity Project/Replication File/Formatted Data/flat_df_for_jeff_w_uncertainty.csv", stringsAsFactors = FALSE)

#create version of sims 
vote.level.sims.median <- ddply(vote.level.sims , .(issue,senator, party, vote, state), function(x) apply(x[,4:33] , 2 , median))





vote.level.sims.median <- within(vote.level.sims.median,{
    label <- issue
    year <- as.numeric(substr(issue, nchar(issue) -4 , nchar(issue) - 1))
    partisan <- ifelse(party == "D",  Dem, Rep) 
    opp.partisan <-   ifelse(party == "D",  Rep, Dem) 
    Q5yes <- Q5 > .5
    Q1yes <- Q1 > .5
    Q3yes <- Q3 > .5
    Pyes <- partisan > .5
    opp.Pyes <- opp.partisan > .5
    repdum <- ifelse(party == "R", 1, 0)
    demdum <- ifelse(party == "D", 1, 0)
    #    party_SW_per <- ifelse(party == "D", Dem_SW_per, Rep_SW_per)
    party_SW_per <- NA
    party.issue.factor <- paste(party, issue)
    SWyes <- SW > .5
    cong <- (SWyes == vote)
    congrich <- (Q5yes == vote)
    congpoor <- (Q1yes == vote)
    congpart <- (Pyes == vote)
    congQ3 <- (Q3yes == vote)
    congopppart <- (opp.Pyes == vote)
    cong.rich.over.part <- ifelse(congrich == congpart, NA, congrich * (1 - congpart))
    cong.poor.over.part <- ifelse(congpoor == congpart, NA, congpoor * (1 - congpart))
    cong.rich.over.poor <- ifelse(congrich == congpoor, NA, congrich * (1 - congpoor))
    share.support.Q5 <- (.2 * Q5) / SW
    share.support.Q4 <- (.2 * Q4) / SW
    share.support.Q3 <- (.2 * Q3) / SW
    share.support.Q2 <- (.2 * Q2) / SW
    share.support.Q1 <- (.2 * Q1) / SW
    rep.control.senate <- ifelse(year <= 2006 | year > 2014, 1, 0)
})


#this is the above re-aggregated to the senator level, averaged over his or her voting record
senator.level.sims.median <- with(vote.level.sims.median,{
    cong <- tapply(cong, senator, mean)
    congrich <- tapply(congrich, senator, mean)
    congpoor <- tapply(congpoor, senator, mean)
    congpart <- tapply(congpart, senator, mean)
    congopppart <- tapply(congopppart, senator, mean)
    cong.rich.over.part <- tapply(cong.rich.over.part, senator, mean, na.rm = TRUE)
    cong.poor.over.part <- tapply(cong.poor.over.part, senator, mean, na.rm = TRUE)
    cong.rich.over.poor <- tapply(cong.rich.over.poor, senator, mean, na.rm = TRUE)
    repdum <- tapply(repdum, senator, max)
    party <- ifelse(repdum == 1, "R", "D")
    partycolor <- ifelse(repdum == 1, "red", "blue")
    party_SW_per <-   tapply(party_SW_per, senator, mean)
    data.frame(cong, congrich, congpoor, congpart, congopppart, cong.rich.over.part, cong.poor.over.part ,cong.rich.over.poor, repdum, party, partycolor, party_SW_per )
})





#this is the vote level data reaggregated to the roll call  or issue level, with currently 39 such roll call
issue.level.sims.median <- with(vote.level.sims.median,{
    Dem <- tapply(Dem, issue, mean)
    Rep <- tapply(Rep, issue, mean)
    SW <- tapply(SW, issue, mean)
    year <- tapply(year, issue, mean)
    rep.control.senate <- tapply(rep.control.senate, issue, mean)
    Q5 <- tapply(Q5, issue, mean)
    Q1 <- tapply(Q1, issue, mean)
    vote.total <- tapply(vote, issue, sum)
    anti.vote.total <- tapply(1 - vote,issue,sum)
    vote.total.dem <- tapply(vote * demdum, issue, sum)
    vote.total.rep <- tapply(vote * repdum, issue, sum)
    num.dems <- tapply(demdum, issue, sum)
    num.reps <- tapply(repdum, issue, sum)
    cong <- tapply(cong, issue, mean)
    congrich <- tapply(congrich, issue, mean)
    congpoor <- tapply(congpoor, issue, mean)
    congpart <- tapply(congpart, issue, mean)
    congopppart <-  tapply(congopppart, issue, mean)
    cong.rich.over.part <- tapply(cong.rich.over.part, issue, mean, na.rm = TRUE)
    cong.poor.over.part <- tapply(cong.poor.over.part, issue, mean, na.rm = TRUE)
    cong.rich.over.poor <- tapply(cong.rich.over.poor, issue, mean, na.rm = TRUE)
    data.frame(year, Dem, Rep, SW, Q5, Q1, vote.total,anti.vote.total,vote.total.dem, vote.total.rep, num.dems, num.reps, cong, congrich, congpoor, congpart, congopppart, cong.rich.over.part, cong.poor.over.part ,cong.rich.over.poor, rep.control.senate )
})

#adds some columns summarizing issue.level.sims.median data
issue.level.sims.median <- within(issue.level.sims.median,{
    vote.yes.dem.percent <- vote.total.dem / num.dems
    vote.yes.rep.percent <- vote.total.rep / num.reps
    opposed.parties <- ifelse((vote.yes.dem.percent > .5) == (vote.yes.rep.percent < .5), TRUE, FALSE)
    more.repvotes.issue <- ifelse(vote.yes.rep.percent > vote.yes.dem.percent, 1, 0)
    more.repsupport.issue <- ifelse(Rep > Dem, 1, 0)
    votes.support.split <- (more.repvotes.issue != more.repsupport.issue)
    more.richsupport.issue <- ifelse(Q5 > Q1, 1, 0)
})

issue.level.sims.median$issue <- row.names(issue.level.sims.median)



#brings some above summaries from issue.level.sims.median back to the vote level data

vote.level.sims.median <- join(vote.level.sims.median,subset(issue.level.sims.median, select = c(issue, more.repvotes.issue,more.richsupport.issue, vote.yes.dem.percent, vote.yes.rep.percent )) , by = "issue")




vote.level.sims.median$vote.for.rich.side <-  ifelse(vote.level.sims.median$more.richsupport.issue == 1, vote.level.sims.median$vote ,  1 - vote.level.sims.median$vote)
vote.level.sims.median$Q5.supportforrichside <- ifelse(vote.level.sims.median$more.richsupport.issue == 1, vote.level.sims.median$Q5 ,  1 - vote.level.sims.median$Q5)
vote.level.sims.median$Q1.supportforrichside <- ifelse(vote.level.sims.median$more.richsupport.issue == 1, vote.level.sims.median$Q1 ,  1 - vote.level.sims.median$Q1)
vote.level.sims.median$partisan.supportforrichside <- ifelse(vote.level.sims.median$more.richsupport.issue == 1, vote.level.sims.median$partisan ,  1 - vote.level.sims.median$partisan)


vote.level.sims.median$vote <- abs(vote.level.sims.median$vote)
vote.level.sims.median <- within(vote.level.sims.median, {
    libdir.Q1 <- ifelse(more.repvotes.issue == 1,   1 - Q1,  Q1)
    libdir.Q5 <- ifelse(more.repvotes.issue == 1,   1 - Q5,  Q5)
    libdir.SW <- ifelse(more.repvotes.issue == 1,   1 - SW,  SW)
    libdir.Dem <- ifelse(more.repvotes.issue == 1,   1 - Dem,  Dem)
    libdir.Rep <- ifelse(more.repvotes.issue == 1,   1 - Rep,  Rep)
    libdir.vote <- ifelse(more.repvotes.issue == 1,   1 - vote,   vote)
    own.party.issue <-  ifelse(party == "R", more.repvotes.issue, 1 - more.repvotes.issue )
    party.line.says.vote <- ifelse(party == "R", ifelse(vote.yes.rep.percent >= .5, 1, 0), ifelse(vote.yes.dem.percent >= .5, 1, 0) )
})



issue.level.sims.median$partisan.diff <-  abs(issue.level.sims.median$Dem - issue.level.sims.median$Rep)
issue.level.sims.median$class.diff <-  abs(issue.level.sims.median$Q5 - issue.level.sims.median$Q1)


vote.level.sims.median$partisan.diff <-  abs(vote.level.sims.median$Dem - vote.level.sims.median$Rep)
vote.level.sims.median$class.diff <-  abs(vote.level.sims.median$Q5 - vote.level$Q1)

 
vote.level.sims.median <- arrange(vote.level.sims.median, issue,senator)




##########################################
######   main data.frame construction done




# Models of responsiveness ------------------------------------------------


#use bglmer for logits, or blmer for reg prob mods


# make version of opinion data rescaled to center close to 50%, to help convergence.  also rescale so a one unit change is 10 points of opinion.


myRescale <- function(x ){
    foo <- (x - .5) *100
    return(foo)
}


vote.level.rescaled <- subset(vote.level, select = c(issue, party, vote, SW, Q5, Q1, partisan, Q5yes, Q1yes, Pyes, more.repvotes.issue  ))
vote.level.rescaled <- within(vote.level.rescaled,{
    SW <- myRescale(SW)
    Q5 <- myRescale(Q5)
    Q1 <- myRescale(Q1)
    partisan <- myRescale(partisan)
})



myDisplay <- function(x.mlm){
	display(x.mlm, detail = TRUE)
	preds <- predict(x.mlm, type = "response")
	pcp <- mean((preds >= .5) == x.mlm@resp$y)
	print("pcp=")
	print(pcp)
	return(pcp)
}

quantile(vote.level.rescaled$Q5)


#  ALL SENATORS



# single op


mlms <- list()


foo <-(bglmer(formula = vote ~
								+ Q5
							#              + Q1
							#             + partisan
							+ (1
								 + Q5
								 #               + Q1
								 #               + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled)))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								#                  + Q5
								+ Q1
							#           + partisan
							+ (1
								 #                + Q5
								 + Q1
								 #               + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled)))


mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								#             + Q5
								#         + Q1
								+ partisan
							+ (1
								 #              + Q5
								 #              + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled)))


mlms <- list.append(mlms, foo)




# pairs of op

foo <-(bglmer(formula = vote ~
								+ Q5
							+ Q1
							#              + partisan
							+ (1
								 + Q5
								 + Q1
								 #                + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled)))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								+ Q5
							#              + Q1
							+ partisan
							+ (1
								 + Q5
								 #               + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled)))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								#                  + Q5
								+ Q1
							+ partisan
							+ (1
								 #                + Q5
								 + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled)))


mlms <- list.append(mlms, foo)


# all three op
foo <-(bglmer(formula = vote ~
								+ Q5
							+ Q1
							+ partisan
							+ (1
								 + Q5
								 + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled)))

mlms <- list.append(mlms, foo)





#  DEMS 



# single op


foo <-(bglmer(formula = vote ~
								+ Q5
							#              + Q1
							#             + partisan
							+ (1
								 + Q5
								 #               + Q1
								 #               + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "D")))



mlms <- list.append(mlms, foo)

foo <-(bglmer(formula = vote ~
								#                  + Q5
								+ Q1
							#           + partisan
							+ (1
								 #                + Q5
								 + Q1
								 #               + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "D")))


mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								#             + Q5
								#         + Q1
								+ partisan
							+ (1
								 #              + Q5
								 #              + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "D")))

mlms <- list.append(mlms, foo)


# pairs of op



foo <-(bglmer(formula = vote ~
                  + Q5
              + Q1
              #              + partisan
              + (1
                 + Q5
                 + Q1
                 #                + partisan
                 |issue) 
              , family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "D")))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								+ Q5
							#              + Q1
							+ partisan
							+ (1
								 + Q5
								 #               + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "D")))


mlms <- list.append(mlms, foo)

foo <-(bglmer(formula = vote ~
								#                  + Q5
								+ Q1
							+ partisan
							+ (1
								 #                + Q5
								 + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "D")))

mlms <- list.append(mlms, foo)



# all three op
foo <-(bglmer(formula = vote ~
								+ Q5
							+ Q1
							+ partisan
							+ (1
								 + Q5
								 + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "D")))

mlms <- list.append(mlms, foo)



#  REPs 



# single op


foo <-(bglmer(formula = vote ~
								+ Q5
							#              + Q1
							#             + partisan
							+ (1
								 + Q5
								 #               + Q1
								 #               + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "R")))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								#                  + Q5
								+ Q1
							#           + partisan
							+ (1
								 #                + Q5
								 + Q1
								 #               + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "R")))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								#             + Q5
								#         + Q1
								+ partisan
							+ (1
								 #              + Q5
								 #              + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "R")))

mlms <- list.append(mlms, foo)


# pairs of op



foo <-(bglmer(formula = vote ~
                  + Q5
              + Q1
              #              + partisan
              + (1
                 + Q5
                 + Q1
                 #                + partisan
                 |issue) 
              , family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "R")))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								+ Q5
							#              + Q1
							+ partisan
							+ (1
								 + Q5
								 #               + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "R")))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								#                  + Q5
								+ Q1
							+ partisan
							+ (1
								 #                + Q5
								 + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "R")))


mlms <- list.append(mlms, foo)



# all three op
foo <-(bglmer(formula = vote ~
								+ Q5
							+ Q1
							+ partisan
							+ (1
								 + Q5
								 + Q1
								 + partisan
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level.rescaled, party == "R")))

mlms <- list.append(mlms, foo)




#   SAME BUT DICHOTOMOUS OPINION



#  ALL SENATORS



# single op


foo <-(bglmer(formula = vote ~
								+ Q5yes
							#              + Q1yes
							#             + Pyes
							+ (1
								 + Q5yes
								 #               + Q1yes
								 #               + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level)))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								#                  + Q5yes
								+ Q1yes
							#           + Pyes
							+ (1
								 #                + Q5yes
								 + Q1yes
								 #               + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level)))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								#             + Q5yes
								#         + Q1yes
								+ Pyes
							+ (1
								 #              + Q5yes
								 #              + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level)))

mlms <- list.append(mlms, foo)

# pairs of op


foo <-(bglmer(formula = vote ~
                  + Q5yes
              + Q1yes
              #              + Pyes
              + (1
                 + Q5yes
                 + Q1yes
                 #                + Pyes
                 |issue) 
              , family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level)))

mlms <- list.append(mlms, foo)



foo <-(bglmer(formula = vote ~
								+ Q5yes
							#              + Q1yes
							+ Pyes
							+ (1
								 + Q5yes
								 #               + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level)))
mlms <- list.append(mlms, foo)

foo <-(bglmer(formula = vote ~
								#                  + Q5yes
								+ Q1yes
							+ Pyes
							+ (1
								 #                + Q5yes
								 + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level)))
mlms <- list.append(mlms, foo)




# all three op
foo <-(bglmer(formula = vote ~
								+ Q5yes
							+ Q1yes
							+ Pyes
							+ (1
								 + Q5yes
								 + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level)))

mlms <- list.append(mlms, foo)



#  DEMS 

# single op


foo <-(bglmer(formula = vote ~
								+ Q5yes
							#              + Q1yes
							#             + Pyes
							+ (1
								 + Q5yes
								 #               + Q1yes
								 #               + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "D")))
mlms <- list.append(mlms, foo)

foo <-(bglmer(formula = vote ~
								#                  + Q5yes
								+ Q1yes
							#           + Pyes
							+ (1
								 #                + Q5yes
								 + Q1yes
								 #               + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "D")))
mlms <- list.append(mlms, foo)

foo <-(bglmer(formula = vote ~
								#             + Q5yes
								#         + Q1yes
								+ Pyes
							+ (1
								 #              + Q5yes
								 #              + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "D")))

mlms <- list.append(mlms, foo)

# pairs of op



foo <-(bglmer(formula = vote ~
                  + Q5yes
              + Q1yes
              #              + Pyes
              + (1
                 + Q5yes
                 + Q1yes
                 #                + Pyes
                 |issue) 
              , family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "D")))

mlms <- list.append(mlms, foo)


foo <-(bglmer(formula = vote ~
								+ Q5yes
							#              + Q1yes
							+ Pyes
							+ (1
								 + Q5yes
								 #               + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "D")))
mlms <- list.append(mlms, foo)

foo <-(bglmer(formula = vote ~
								#                  + Q5yes
								+ Q1yes
							+ Pyes
							+ (1
								 #                + Q5yes
								 + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "D")))
mlms <- list.append(mlms, foo)



# all three op
foo <-(bglmer(formula = vote ~
								+ Q5yes
							+ Q1yes
							+ Pyes
							+ (1
								 + Q5yes
								 + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "D")))



mlms <- list.append(mlms, foo)



#  REPs 


# single op


foo <-(bglmer(formula = vote ~
								+ Q5yes
							#              + Q1yes
							#             + Pyes
							+ (1
								 + Q5yes
								 #               + Q1yes
								 #               + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "R")))
mlms <- list.append(mlms, foo)

foo <-(bglmer(formula = vote ~
								#                  + Q5yes
								+ Q1yes
							#           + Pyes
							+ (1
								 #                + Q5yes
								 + Q1yes
								 #               + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "R")))
mlms <- list.append(mlms, foo)

foo <-(bglmer(formula = vote ~
								#             + Q5yes
								#         + Q1yes
								+ Pyes
							+ (1
								 #              + Q5yes
								 #              + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "R")))
mlms <- list.append(mlms, foo)


# pairs of op



foo <-(bglmer(formula = vote ~
                  + Q5yes
              + Q1yes
              #              + Pyes
              + (1
                 + Q5yes
                 + Q1yes
                 #                + Pyes
                 |issue) 
              , family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "R")))
mlms <- list.append(mlms, foo)



foo <-(bglmer(formula = vote ~
								+ Q5yes
							#              + Q1yes
							+ Pyes
							+ (1
								 + Q5yes
								 #               + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "R")))
mlms <- list.append(mlms, foo)

foo <-(bglmer(formula = vote ~
								#                  + Q5yes
								+ Q1yes
							+ Pyes
							+ (1
								 #                + Q5yes
								 + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "R")))
mlms <- list.append(mlms, foo)




# all three op
foo <-(bglmer(formula = vote ~
								+ Q5yes
							+ Q1yes
							+ Pyes
							+ (1
								 + Q5yes
								 + Q1yes
								 + Pyes
								 |issue) 
							, family=binomial(link="logit"), control = glmerControl(optimizer ="bobyqa"), data = subset(vote.level, party == "R")))


mlms <- list.append(mlms, foo)


all.mlms <- llply(mlms, myDisplay)


sink("~/Documents/Dropbox/RSF Ineqaulity Project/Replication File/regressions120418.txt")
for(i in 1:length(mlms)){
	myDisplay(mlms[[i]])

}
sink()










     